﻿using MyProduce.enumeration;
using MyProduce.MyProduce.ProduceDto;
using MyProduce.Produce;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace MyProduce.MyProduce
{

    /// <summary>
    /// 羊只档案
    /// </summary>
    public class GrowthstageAppService : ApplicationService, IGrowthstageAppService
    {
        private readonly IRepository<Growthstage, int> _growthstageRepository;
        private readonly IRepository<SheepManagement, int> _sheepManagementRepository;
        public readonly IRepository<Birth, int> _birthRepository;
        public readonly IRepository<Presence, int> _presenceRepository;
        public readonly IHttpClientFactory _httpClientFactory;
        public GrowthstageAppService(IRepository<Growthstage, int> growthstageRepository, IRepository<SheepManagement, int> sheepManagementRepository, IRepository<Birth, int> birthRepository, IRepository<Presence, int> presenceRepository, IHttpClientFactory httpClientFactory)
        {
            _growthstageRepository = growthstageRepository;
            _sheepManagementRepository = sheepManagementRepository;
            _birthRepository = birthRepository;
            _presenceRepository = presenceRepository;
            _httpClientFactory = httpClientFactory;
        }


      
        /// <summary>
        /// 生长阶段
        /// </summary>
        /// <returns></returns>
        public async Task<List<GrowthstageDto>> GetListGrowthstageAsync()
        {
            var result = await _growthstageRepository.GetListAsync();
            return ObjectMapper.Map<List<Growthstage>, List<GrowthstageDto>>(result);
        }

        public async Task<List<BirthTypeDto>> GetBirthShow()
        {
            var result = await _birthRepository.GetListAsync();
            return ObjectMapper.Map<List<Birth>, List<BirthTypeDto>>(result);
        }
        /// <summary>
        /// 在场状态
        /// </summary>
        /// <returns></returns>
        public async Task<List<presenceDto>> GetpresenceShow()
        {
            var result = await _presenceRepository.GetListAsync();
            return ObjectMapper.Map<List<Presence>, List<presenceDto>>(result);
        }
        /// <summary>
        /// 枚举显示羊只类型
        /// </summary>
        /// <returns></returns>
        public async Task<List<SheepTypeEnumItemDto>> GetSheeptypeEnumShow()
        {
            return await Task.Run(() =>
            {
                List<SheepTypeEnumItemDto> list = new List<SheepTypeEnumItemDto>();

                foreach (var item in Enum.GetValues(typeof(SheeptypeEnum)))
                {
                    list.Add(new SheepTypeEnumItemDto
                    {
                        Name = item.ToString(),
                        TId = Convert.ToInt32(item)
                    });
                }

                return list;
            });
        }
        /// <summary>
        /// 显示羊只档案
        /// </summary>
        /// <param name="Overbit"></param>
        /// <param name="Variety"></param>
        /// <param name="Growthstage"></param>
        /// <param name="Presence"></param>
        /// <param name="Reproductivestate"></param>
        /// <param name="StartDate"></param>
        /// <param name="EndDate"></param>
        /// <returns></returns>
        public async Task<List<SheepManagementShowDto>> GetSheepShow(string? Overbit, string? Variety, string? Growthstage, string? Presence, string? Reproductivestate, DateTime? StartDate, DateTime? EndDate)
        {
            var result = await _sheepManagementRepository.GetListAsync();
            var sheeclass = await GetSheeptypeEnumShow();
            var Geneclass = await GetGeneclassEnumShow();
            var SexClass= await GetSexEnumShow();
            var list = (from a in result
                        join b in sheeclass on a.Sheeptype equals b.TId
                        join c in Geneclass on a.Geneclass equals c.GId
                        join d in SexClass on a.Sex equals d.SId
                        select new SheepManagementShowDto
                        {
                            Id = a.Id,
                            Geneclass = a.Geneclass,
                            Reproductivestate = a.Reproductivestate,
                            Variety = a.Variety,
                            SheeTypeNaame = b.Name,
                            Cottage = a.Cottage,
                            Field = a.Field,
                            Growthstage = a.Growthstage,
                            Sex = a.Sex,
                            Productiongrade = a.Productiongrade,
                            Salesgrade = a.Salesgrade,
                            Presence = a.Presence,
                            Fatherear = a.Fatherear,
                            Motherearhorn = a.Motherearhorn,
                            Dateofbirth = a.Dateofbirth,
                            Admissiondate = a.Admissiondate,
                            Overbit = a.Overbit,
                            Placeofbirth = a.Placeofbirth,
                            Birthweightbirthweight = a.Birthweightbirthweight,
                            Remark = a.Remark,
                            GeneclassName = c.GName,
                            BirthStatus = a.BirthStatus,
                            Sheeptype = a.Sheeptype,
                            SexName=d.SName
                        }).ToList();
            if (!string.IsNullOrEmpty(Overbit))
            {
                list = list.Where(x => x.Overbit==Overbit).ToList();
            }
            if (!string.IsNullOrEmpty(Variety))
            {
                list = list.Where(x => x.Variety.Contains(Variety)).ToList();
            }
            if (!string.IsNullOrEmpty(Growthstage))
            {
                list = list.Where(x => x.Growthstage.Contains(Growthstage)).ToList();
            }
            if (!string.IsNullOrEmpty(Presence))
            {
                list = list.Where(x => x.Presence.Contains(Presence)).ToList();
            }
            if (!string.IsNullOrEmpty(Reproductivestate))
            {
                list = list.Where(x => x.Reproductivestate.Contains(Reproductivestate)).ToList();
            }
            if (StartDate != null)
            {
                list = list.Where(x => x.Admissiondate >= StartDate).ToList();
            }
            if (EndDate != null)
            {
                list = list.Where(x => x.Admissiondate <= EndDate).ToList();
            }
            return list;
        }
        public async Task<List<SheepManagementShowDto>> GetSheepShowg()
        { 
            var result = await _sheepManagementRepository.GetListAsync();
            var sheeclass = await GetSheeptypeEnumShow();
            var Geneclass = await GetGeneclassEnumShow();
            var SexClass = await GetSexEnumShow();
            var list = (from a in result
                        join b in sheeclass on a.Sheeptype equals b.TId
                        join c in Geneclass on a.Geneclass equals c.GId
                        join d in SexClass on a.Sex equals d.SId
                        where(a.Sheeptype==1)
                        select new SheepManagementShowDto
                        {
                            Id = a.Id,
                            Geneclass = a.Geneclass,
                            Reproductivestate = a.Reproductivestate,
                            Variety = a.Variety,
                            SheeTypeNaame = b.Name,
                            Cottage = a.Cottage,
                            Field = a.Field,
                            Growthstage = a.Growthstage,
                            Sex = a.Sex,
                            Productiongrade = a.Productiongrade,
                            Salesgrade = a.Salesgrade,
                            Presence = a.Presence,
                            Fatherear = a.Fatherear,
                            Motherearhorn = a.Motherearhorn,
                            Dateofbirth = a.Dateofbirth,
                            Admissiondate = a.Admissiondate,
                            Overbit = a.Overbit,
                            Placeofbirth = a.Placeofbirth,
                            Birthweightbirthweight = a.Birthweightbirthweight,
                            Remark = a.Remark,
                            GeneclassName = c.GName,
                            BirthStatus = a.BirthStatus,
                            Sheeptype = a.Sheeptype,
                            SexName = d.SName
                        }).ToList();
        
            return list;
        }
        public async Task<List<SheepManagementShowDto>> GetSheepShowm()
        {
            var result = await _sheepManagementRepository.GetListAsync();
            var sheeclass = await GetSheeptypeEnumShow();
            var Geneclass = await GetGeneclassEnumShow();
            var SexClass = await GetSexEnumShow();
            var list = (from a in result
                        join b in sheeclass on a.Sheeptype equals b.TId
                        join c in Geneclass on a.Geneclass equals c.GId
                        join d in SexClass on a.Sex equals d.SId
                        where (a.Sheeptype == 2)
                        select new SheepManagementShowDto
                        {
                            Id = a.Id,
                            Geneclass = a.Geneclass,
                            Reproductivestate = a.Reproductivestate,
                            Variety = a.Variety,
                            SheeTypeNaame = b.Name,
                            Cottage = a.Cottage,
                            Field = a.Field,
                            Growthstage = a.Growthstage,
                            Sex = a.Sex,
                            Productiongrade = a.Productiongrade,
                            Salesgrade = a.Salesgrade,
                            Presence = a.Presence,
                            Fatherear = a.Fatherear,
                            Motherearhorn = a.Motherearhorn,
                            Dateofbirth = a.Dateofbirth,
                            Admissiondate = a.Admissiondate,
                            Overbit = a.Overbit,
                            Placeofbirth = a.Placeofbirth,
                            Birthweightbirthweight = a.Birthweightbirthweight,
                            Remark = a.Remark,
                            GeneclassName = c.GName,
                            BirthStatus = a.BirthStatus,
                            Sheeptype = a.Sheeptype,
                            SexName = d.SName
                        }).ToList();

            return list;
        }
        /// <summary>
        /// 枚举显示基因等级
        /// </summary>
        /// <returns></returns>

        public async Task<List<GetGeneDto>> GetGeneclassEnumShow()
        {

            return await Task.Run(() =>
            {
                List<GetGeneDto> list = new List<GetGeneDto>();

                foreach (var item in Enum.GetValues(typeof(GeneclassEnum)))
                {
                    list.Add(new GetGeneDto
                    {
                        GName = item.ToString(),
                        GId = Convert.ToInt32(item)
                    });
                }

                return list;
            });
        }
        /// <summary>
        /// 枚举显示性别
        /// </summary>
        /// <returns></returns>

        public async Task<List<SexEnumDto>> GetSexEnumShow()
        {
            return await Task.Run(() =>
            {
                List<SexEnumDto> list = new List<SexEnumDto>();

                foreach (var item in Enum.GetValues(typeof(SexEnum)))
                {
                    list.Add(new SexEnumDto
                    {
                        SName = item.ToString(),
                        SId = Convert.ToInt32(item)
                    });
                }
                return list;
            });
        }
        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SheepManagementDto> GetSheepFindaa(int eid)
        {
          var result= await _sheepManagementRepository.GetAsync(eid);
         return ObjectMapper.Map<SheepManagement, SheepManagementDto>(result);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Aid"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int Aid)
        {
            await _sheepManagementRepository.DeleteAsync(Aid);
        }

        public async Task<EarResult<SheepManagementDto>> SheepAdd(SheepManagement input)
        {

            var existingSheep = await _sheepManagementRepository.FirstOrDefaultAsync(x => x.Overbit == input.Overbit); // 假设使用Fatherear作为耳号
            if (existingSheep == null)
            {
                SheepManagement sheep = new SheepManagement();

                sheep.Variety = input.Variety;
                sheep.Geneclass = input.Geneclass;
                sheep.Reproductivestate = input.Reproductivestate;
                sheep.Cottage = input.Cottage;
                sheep.Field = input.Field;
                sheep.Growthstage = input.Growthstage;
                sheep.Sex = input.Sex;
                sheep.Sheeptype = input.Sheeptype;
                sheep.Productiongrade = input.Productiongrade;
                sheep.Salesgrade = input.Salesgrade;
                sheep.Presence = input.Presence;
                sheep.Fatherear = input.Fatherear;
                sheep.Motherearhorn = input.Motherearhorn;
                sheep.Dateofbirth = input.Dateofbirth;
                sheep.Admissiondate = input.Admissiondate;
                sheep.Overbit = input.Overbit;
                sheep.Placeofbirth = input.Placeofbirth;
                sheep.Birthweightbirthweight = input.Birthweightbirthweight;
                sheep.Remark = input.Remark;
                sheep.BirthStatus = input.BirthStatus;
                var result = await _sheepManagementRepository.InsertAsync(sheep);
                var res = ObjectMapper.Map<SheepManagement, SheepManagementDto>(result);
                if (res != null)
                {
                    return new EarResult<SheepManagementDto>() { Code = 200, Message = "添加成功" };
                }
                else
                {
                    return new EarResult<SheepManagementDto>() { Code = 500, Message = "添加失败" };
                }
            }
            else
            {
                return new EarResult<SheepManagementDto>() { Code = 501, Message = "耳号已存在" };
            }
        }
        //public async Task<List<ErDto>> GetErShow()
        //{
        //    var list=await  _sheepManagementRepository.GetListAsync();
        //    var clien = _httpClientFactory.CreateClient();
        //    var result = await clien.GetAsync("http://www.sojson.com/open/api/weather/json.shtml?city=北京");

        //}
    }
}

        
  

